package com.ciwong.epaper.common.dialogbottom;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import com.android.volley.TimeoutError;
import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.epaper.bean.CatalogueInfo;
import com.ciwong.epaper.modules.epaper.bean.Node;
import com.ciwong.epaper.modules.epaper.dao.EpaperDao;
import com.ciwong.epaper.modules.me.adapter.CatalogDialogAdapter;
import com.ciwong.epaper.modules.me.bean.BooksPage;
import com.ciwong.epaper.util.ViewUtil;
import com.ciwong.epaper.widget.DividerItemDecoration;
import com.ciwong.mobilelib.i.BaseCallBack;
import com.ciwong.mobilelib.utils.ToastUtil;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class CatalogBottomDialog extends BaseBottomDialog {
    private RecyclerView rvDialogCatalog;
    private ImageView favorive_book_img;
    private ItemCallBack itemCallBack;
    private CatalogDialogAdapter catalogDialogAdapter;
    private int isTop;
    private LinkedList<Node> mLinkedList = new LinkedList<>();
    private BooksPage mBooksPage;

    public interface ItemCallBack {
        void onItemCallBack(CatalogueInfo catalogueInfo);
    }

    public void setOnItemCallBackListener(ItemCallBack itemCallBack) {
        this.itemCallBack = itemCallBack;
    }

    public CatalogBottomDialog(Context context, int style) {
        super(context, style);
    }

    @Override
    public void initView(Context context) {
        initViewClick();
        initRecycle(context);
    }
    public void initViewClick() {
        View mRootView = LayoutInflater.from(this.getContext()).inflate(R.layout.fragment_catalog_dialog, null);
        mRootView.findViewById(R.id.tl_close).setOnClickListener(this);
        rvDialogCatalog = mRootView.findViewById(R.id.rv_dialog_catalog);
        favorive_book_img = mRootView.findViewById(R.id.favorive_book_img);
        setContentView(mRootView);
    }

    public void initRecycle(Context context) {
        if (catalogDialogAdapter == null)
            catalogDialogAdapter = new CatalogDialogAdapter(context);
        if (rvDialogCatalog == null) return;
        rvDialogCatalog.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        rvDialogCatalog.setAdapter(catalogDialogAdapter);
        rvDialogCatalog.addItemDecoration(new DividerItemDecoration(context, LinearLayoutManager.VERTICAL, ViewUtil.dip2px(context, (float) 0.5), R.color.def_line_color));
        favorive_book_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int serviceId = 0;
                if (null != mBooksPage.getServiceId() && !"".equals(mBooksPage.getServiceId())){
                    serviceId = Integer.valueOf(mBooksPage.getServiceId()) ;
                }
                if (isTop == 1){
                    setUserBookTop(0,mBooksPage.getProductId(),serviceId);
                }else {
                    setUserBookTop(1,mBooksPage.getProductId(),serviceId);
                }
            }
        });

        catalogDialogAdapter.setOnItemClickListener(new CatalogDialogAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView.ViewHolder holder, List<Node> list, int position) {
                dismiss();
                Node baseNode = (Node) list.get(position);
                if (itemCallBack != null)
                    itemCallBack.onItemCallBack(baseNode.getCatalogueInfo());
            }
        });
        catalogDialogAdapter.setData(mLinkedList);

    }

    public void setUserBookTop(final int istop, int productid, int serviceid) {
        EpaperDao.getInstance().setUserBookTop(istop, productid, serviceid, new BaseCallBack() {
            @Override
            public void failed(int errorCode, Object data) {
                if (data instanceof TimeoutError) {
                    //hideCricleProgress();
                    if (isTop == 1) {
                        ToastUtil.INSTANCE.toastCenterError(getContext().getString(R.string.cancel_favorite_faild_check_network));
                    } else {
                        ToastUtil.INSTANCE.toastCenterError(getContext().getString(R.string.favorite_faild_check_network));
                    }

                } else {
                    if (isTop == 1) {
                        ToastUtil.INSTANCE.toastCenterError(getContext().getString(R.string.cancel_favorite_faild_try_later));
                    } else {
                        ToastUtil.INSTANCE.toastCenterError(getContext().getString(R.string.favorite_faild_try_later));
                    }
                }
            }

            @Override
            public void failed(Object data) {
                if (isTop == 1) {
                    ToastUtil.INSTANCE.toastCenterError(getContext().getString(R.string.cancel_favorite_faild_try_later));
                } else {
                    ToastUtil.INSTANCE.toastCenterError(getContext().getString(R.string.favorite_faild_try_later));
                }
            }

            @Override
            public void success() {
                super.success();
                if (isTop == 1) {
                    ToastUtil.INSTANCE.toastCenterSuccess(getContext().getString(R.string.cancel_favoeite_success));
                    isTop = 0;
                    mBooksPage.setIstop(0);
                    favorive_book_img.setImageResource(R.drawable.no_favorites_icon);
                } else {
                    isTop = 1;
                    ToastUtil.INSTANCE.toastCenterSuccess(getContext().getString(R.string.favoeite_success));
                    mBooksPage.setIstop(1);
                    favorive_book_img.setImageResource(R.drawable.is_favorites_icon);
                }
            }
        });
    }


    /**
     * 更新数据
     *
     * @param CatalogueInfo
     * @param pid
     */
    public void initData(List<CatalogueInfo> CatalogueInfo, int pid, BooksPage booksPage) {
        mBooksPage = booksPage;
        if (null != mBooksPage){
            isTop = mBooksPage.getIstop();
        }
        if (isTop == 1){
            favorive_book_img.setImageResource(R.drawable.is_favorites_icon);
        }else {
            favorive_book_img.setImageResource(R.drawable.no_favorites_icon);
        }
        if (CatalogueInfo == null || CatalogueInfo.size() <= 0) return;
        List<Node> baseNodes = constructData(CatalogueInfo, pid);
        mLinkedList.clear();
        //mLinkedList.addAll(NodeHelp.sortNode(baseNodes, true));//整理关系
        mLinkedList.addAll(baseNodes);
        if (catalogDialogAdapter != null)
            catalogDialogAdapter.setData(mLinkedList);
    }

    private List<Node> constructData(List<CatalogueInfo> CatalogueInfo, int pId) {
        ArrayList<Node> list = new ArrayList();
        if (pId == 0) pId = -1;
        return handleData(CatalogueInfo, list, pId);
    }

    /**
     * 构造数据
     *
     * @param catalogueUnitBeans
     * @param list
     * @param pId
     * @return
     */
    private List<Node> handleData(List<CatalogueInfo> catalogueUnitBeans, List<Node> list, int pId) {
        Node catalogNode = null;
        for (int i = 0; i < catalogueUnitBeans.size(); i++) {
            CatalogueInfo CatalogueInfo = catalogueUnitBeans.get(i);
            String idself = CatalogueInfo.getId() == null ? 0 + "" : CatalogueInfo.getId();
            catalogNode = new Node();
            catalogNode.setpId(pId);
            catalogNode.setId(Integer.valueOf(idself));
            catalogNode.setCatalogueInfo(CatalogueInfo);
            list.add(catalogNode);
            if (CatalogueInfo.getChildren() != null && CatalogueInfo.getChildren().size() > 0) {
                String idself_ = CatalogueInfo.getId() == null ? 0 + "" : CatalogueInfo.getId();
                handleData(CatalogueInfo.getChildren(), list, Integer.valueOf(idself_));
            }
        }
        return list;
    }

}
